from sklearn.preprocessing import MinMaxScaler
import models.connect_db as db
from scipy.stats import pearsonr
import numpy as np
import math

def getIaqi(data):
    """
    将拿到的数据归一化，并计算出坐标
    :return:
    """
    transfer = MinMaxScaler(feature_range=[0, 1])
    data_new = transfer.fit_transform(data)
    M = 0.5
    city_v = []
    for item in data_new:
        v = []
        k = []
        for i in range(6):
            k.append(M * (math.cos(math.pi / 2 * item[i]) + 1))
        v1 = item[0] * math.cos(math.pi / 3) * k[0] - item[1] * math.cos(math.pi / 3) * k[1] - item[2] * math.cos(
            math.pi / 3) * k[2] + item[3] * math.cos(math.pi / 3) * k[3] + item[4] * k[4] - item[5] * k[5]
        v2 = -item[0] * math.sin(math.pi / 3) * k[0] - item[1] * math.sin(math.pi / 3) * k[1] + item[2] * math.sin(
            math.pi / 3) * k[2] + item[3] * math.sin(math.pi / 3) * k[3]
        v.append(v1)
        v.append(v2)
        city_v.append(v)
    return city_v

def cal_relation(index1, index2):
    p = pearsonr(index1, index2)
    if np.isnan(p[0]) == False:
        return p
    else:
        return 0

def cal_cluster_relation(data):
    KEYS = ['PM25(微克每立方米)', 'PM10(微克每立方米)', 'SO2(微克每立方米)', 'NO2(微克每立方米)', 'CO(毫克每立方米)',
              'O3(微克每立方米)', 'RH(%)', 'PSFC(Pa)', '风速', '风向', '摄氏温度']
    relation = {}
    index_data = {}
    for i in range(len(KEYS)):
        indexs = []
        for m in range(len(data)):
            indexs.append(data[m][KEYS[i]])
        index_data[KEYS[i]] = indexs

    for i in range(len(KEYS)):
        for j in range(i+1, len(KEYS)):
            index1 = index_data[KEYS[i]]
            index2 = index_data[KEYS[j]]
            p = cal_relation(index1, index2)
            relation[KEYS[i] + '-' + KEYS[j]] = p
    return relation

def get_city_index(date, type):

    if (type == 'week'):
        sql_str = "select * from city_all where date between '" + date + "' and DATE_ADD('"+ date +"', INTERVAL 6 DAY)"
    if (type == 'month'):
        sql_str = "select * from city_all where date_format(date, '%Y-%m')='" + date + "'"
    if (type == 'quarter'):
        sql_str = "select * from city_all where date_format(date, '%Y-%m')='" + date[0] + "' or date_format(date, '%Y-%m')='"
        + date[1] + "' or date_format(date, '%Y-%m')='" + date[2] + "'"
    return db.excute_query(sql_str)


def cal_average(data, num):
    res = [0]*12
    for item in data:
        res[0] = res[0] + item['PM2.5(微克每立方米)']
        res[1] = res[1] + item['PM10(微克每立方米)']
        res[2] = res[2] + item['SO2(微克每立方米)']
        res[3] = res[3] + item['NO2(微克每立方米)']
        res[4] = res[4] + item['CO(毫克每立方米)']
        res[5] = res[5] + item['O3(微克每立方米)']
        res[6] = res[6] + item['RH(%)']
        res[7] = res[7] + item['PSFC(Pa)']
        res[8] = res[8] + item['AQI']
        res[9] = res[9] + item['风速']
        res[10] = res[10] + item['风向']
        res[11] = res[11] + item['摄氏温度']
    return [round(i / num, 2) for i in res]

def cal_average1(data, num):
    res = [0]*6
    for item in data:
        res[0] = res[0] + item['PM2.5(微克每立方米)']
        res[1] = res[1] + item['PM10(微克每立方米)']
        res[2] = res[2] + item['SO2(微克每立方米)']
        res[3] = res[3] + item['NO2(微克每立方米)']
        res[4] = res[4] + item['CO(毫克每立方米)']
        res[5] = res[5] + item['O3(微克每立方米)']
    return [round(i / num, 2) for i in res]